
/**
 * 按库存明细唯一维度，合并不同的sn
 * 最终格式{code:XXX, name:XXX , snVoList: [{snId:XXX, sn:"1245678" }]}
 * @param {*} records
 */
export const mergeSnVoList = function (records) {
  const result = []
  records.forEach(item => {
    const snId = item.snId
    const sn = item.sn
    const quantity = Number(item.quantity)
    const targetRow = findTheSameRowInList(item, result)
    // 解释：sourceId为来源id
    const snVo = { sourceId: item.id, snId, sn, quantity }
    if (targetRow === null) {
      // 准备插入的行
      const newRow = { ...item }
      newRow.snVoList = [snVo]
      result.push(newRow)
    } else {
      // 修改行即可
      const snVoList = targetRow.snVoList
      snVoList.push(snVo)
      // 匹数
      targetRow.piecesCount = Number(targetRow.piecesCount) + 1
      targetRow.quantity = Number(targetRow.quantity) + quantity
      // 可发货
      targetRow.canDeliverUsePiecesCount = Number(targetRow.canDeliverUsePiecesCount) + 1
      targetRow.canDeliverUseQuantity = Number(targetRow.canDeliverUseQuantity) + quantity
    }
  })
  return result
}

/**
 * 在rowList中查找一样的 row
 * @param {查找和这个row一样的} row
 * @param {在这个列表里查找} rowList
 */
export const findTheSameRowInList = function (row, rowList = []) {
  for (let i = 0; i < rowList.length; i++) {
    if (isTheSameStockDetailUnique(row, rowList[i])) {
      return rowList[i]
    }
  }
  return null
}

/**
 * 两个sn是否是同一个库存明细
 * @param {库存数量明细唯一属性} stockUnique1
 * @param {库存数量明细唯一属性} stockUnique1
 */
export const isTheSameStockDetailUnique = function (stockUnique1, stockUnique2) {
  // 1. spuId
  if (stockUnique1.spuId !== stockUnique2.spuId) {
    return false
  }
  // 2. colorId
  if (stockUnique1.colorId !== stockUnique2.colorId) {
    return false
  }
  // 3. unitId
  if (stockUnique1.unitId !== stockUnique2.unitId) {
    return false
  }
  // 4. storeId
  if (stockUnique1.storeId !== stockUnique2.storeId) {
    return false
  }
  // 5. 幅宽
  if (isBlank(stockUnique1.widthValue) && isNotBlank(stockUnique2.widthValue)) {
    console.log('幅宽不同')
    return false
  }
  if (isNotBlank(stockUnique1.widthValue) && stockUnique1.widthValue !== stockUnique2.widthValue) {
    console.log('幅宽不同')
    return false
  }
  // 6. 克重
  if (stockUnique1.gramWeightValue == null && stockUnique2.gramWeightValue != null) {
    console.log('克重不同')
    return false
  }
  if (stockUnique1.gramWeightValue != null && stockUnique1.gramWeightValue !== stockUnique2.gramWeightValue) {
    console.log('克重不同')
    return false
  }
  // 7. 规格
  if (isBlank(stockUnique1.spec) && isNotBlank(stockUnique2.spec)) {
    console.log('规格不同')
    return false
  }
  if (isNotBlank(stockUnique1.spec) && stockUnique1.spec !== stockUnique2.spec) {
    console.log('规格不同')
    return false
  }
  // 库存明细
  // 8. 缸号
  if (isBlank(stockUnique1.vatNo) && isNotBlank(stockUnique2.vatNo)) {
    console.log('缸号不同')
    return false
  }
  if (isNotBlank(stockUnique1.vatNo) && stockUnique1.vatNo !== stockUnique2.vatNo) {
    console.log('缸号不同')
    return false
  }
  // 9. 卷号
  if (isBlank(stockUnique1.volumeNo) && isNotBlank(stockUnique2.volumeNo)) {
    console.log('volumeNo不同')
    return false
  }
  if (isNotBlank(stockUnique1.volumeNo) && stockUnique1.volumeNo !== stockUnique2.volumeNo) {
    console.log('volumeNo不同')
    return false
  }
  // 10. 批号
  if (isBlank(stockUnique1.batchNumber) && isNotBlank(stockUnique2.batchNumber)) {
    console.log('批号不同')
    return false
  }
  if (isNotBlank(stockUnique1.batchNumber) && stockUnique1.batchNumber !== stockUnique2.batchNumber) {
    console.log('批号不同')
    return false
  }
  // 11. 仓位
  if (isNull(stockUnique1.positionId) && isNotNull(stockUnique2.positionId)) {
    return false
  }
  if (isNotNull(stockUnique1.positionId) && stockUnique1.positionId !== stockUnique2.positionId) {
    return false
  }
  return true
}

function isNull (val) {
  if (val === null || val === undefined) return true
  return false
}

function isNotNull (val) {
  return !isNull(val)
}

function isBlank (val) {
  if (val === '') return true
  if (val === null || val === undefined) return true
  return false
}

function isNotBlank (val) {
  return !isBlank(val)
}
